package com.wwj.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wwj.admin.Vo.UserVo;
import com.wwj.admin.constant.ConstantFinal;
import com.wwj.admin.entity.UserEntity;
import com.wwj.admin.service.UserService;
import com.wwj.common.constant.CommonCodeEnum;
import com.wwj.common.constant.CommonConstant;
import com.wwj.common.entitiy.R;
import com.wwj.common.utils.JWTUtils;
import com.wwj.common.utils.TokenCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 微信小程序用户登录
     * http://127.0.0.1:88/api/user/login
     */
    @PostMapping("/login")
    private R<Object> login(@RequestBody UserVo userVo){
        // 1、接收表单
        // 2、查询数据库身份必须是3之外的
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ConstantFinal.FIELD_USER_ID, userVo.getUserId())
                        .eq(ConstantFinal.FIELD_USER_PASSWORD, userVo.getUserPassword())
                        .ne(ConstantFinal.FIELD_USER_IDENTITY, ConstantFinal.VALUE_ADMIN);
        UserEntity userEntity = userService.getOne(queryWrapper);
        if (userEntity == null) {
            log.info("==========登录失败==========");
            return R.error(CommonCodeEnum.USER_NO_EXIST.getCode(),CommonCodeEnum.USER_NO_EXIST.getMsg());
        }else {
            // 3、有身份信息下发token
            // todo 暂时使用admin的token签名代替
            JWTUtils jwtUtils = new JWTUtils();
            jwtUtils.setSignature(CommonConstant.ADMIN_TOKEN); // 设置签名
            String userToken = jwtUtils.createUserToken(userEntity.getUserId()); // 生成token
            log.info("==========登录成功==========");
            return R.ok(CommonCodeEnum.SUCCESS.getCode(),CommonCodeEnum.SUCCESS.getMsg(),
                    new UserVo(userEntity.getUserId(),
                            userEntity.getUserPassword(),
                            userEntity.getUserIdentity(),
                            userEntity.getUserName(),
                            userToken,
                            null,
                            null));
        }
    }

    /**
     * 获取用户的个人信息数据
     * http://192.168.0.115:88/api/user/getUserData
     */
    @GetMapping("/getUserData")
    private R<Object> getUserData(HttpServletRequest request){
        // 解析token
        String token = request.getHeader(CommonConstant.REQUEST_HEADER_TOKEN);
        // 解析该token的用户id
        String userId = TokenCheck.getUserIdByToken(token);
        // 查询数据
        UserEntity userEntity = userService.getById(userId);
        return R.ok(CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getMsg(), userEntity);
    }

    /**
     * 更新数据
     * http://192.168.0.115:88/api/user/update
     */
    @PostMapping("/update")
    private R<Object> update(@RequestBody UserEntity userEntity){
        System.out.println(userEntity);
        boolean flag = userService.updateById(userEntity);
        if (flag){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }

    /**
     * 登录之前验证token的时效性，时效符合则无需登录
     * http://192.168.0.115:88/api/user/initLogin
     */
    @GetMapping("/initLogin")
    private R<Object> initLogin(HttpServletRequest request){
        String token = request.getHeader(CommonConstant.REQUEST_HEADER_TOKEN);
        boolean flag = TokenCheck.checkToken(token);
        if (flag){
            return R.ok("token有效");
        }else{
            return R.error("token过期");
        }
    }
}
